class CboxHandler {
    private static _i: CboxHandler;
    public static get i(): CboxHandler {

        if (!this._i) {
            this._i = new CboxHandler();
        }

        return this._i;
    }

    private host: CreatePanel;

    constructor() {

    }



    public setHost(host: CreatePanel) {
        this.host = host;
    }

    private cBoxConfig = {
        //互斥组
        group: {
            1: {
                btns: [0, 1],
                //默认选中 0 没有 1 2 3
                def: 2
            }
        },
        //先决条件组
        pre: {
            1: [1]
        },
        //未选中 以此条件为先决 清理
        clear: {
            1: [2]
        },
        //未选中 清理帮助 非互斥组清理
        clearPlus: {

        },
        //选中 清理 不定项先觉条件的选项
        clearHandle: {

        }
        ,
        check: {

        },
        /**
         * premise:先决条件 
         * clearId:以此为先决条件取消选中 
         * group 互斥组
         * checkId:先决组 有其中一项选中则成立
         * 
         * 0: {
                premise: 0,
                clearId: 0,
                group: [1],
                checkId: 0,
                clearP: 0,
                clearH: 0,
            }
         */
        btns: {

        }
    }
    /**
     * 联动checkbox
     * 
    */
    public cBoxSelectHandle(e: egret.TouchEvent) {
        const keys: string[] = Object.keys(this.cBoxConfig.btns);
        const target: eui.CheckBox = e.currentTarget;
        const idx: string = target.name;
        const select: boolean = target.selected;

        if (keys.length && keys.indexOf(idx) > -1) {

            const config = this.cBoxConfig.btns[idx];

            const groupId: number[] = config.group;
            const preId: number = config.premise;
            const clearId: number = config.clearId;
            const checkId: number = config.checkId;
            const clearP: number = config.clearP;
            const clearH: number = config.clearH;
            this.cBoxGroupHandle(groupId, +idx, select);

            if (select) {

                if (preId) {
                    this.cBoxPreHandle(preId);
                }

                if (clearH) {
                    this.cBoxClearhandle(clearH, 3);
                }

            } else {

                if (clearId) {

                    this.cBoxClearhandle(clearId);

                };

                if (clearP) {

                    this.cBoxClearhandle(clearP, 2);
                }

            }

            this.cBoxPresCheckHandle(+idx);

        }

    }
    /**先决条件选中 */
    private cBoxPreHandle(preId: number) {

        let preGroup = this.cBoxConfig.pre[preId];

        preGroup.forEach((num) => {
            const box: eui.CheckBox = this.host[`btn_rule_${num}`];
            if (box) {
                box.selected = true;

                let config = this.cBoxConfig.btns[num];

                if (config.group) {

                    this.cBoxGroupHandle(config.group, num, true);

                }
            }
        });
    }
    /**清理项 */
    private cBoxClearhandle(clearId: number, clearType: number = 1) {

        let clearGroup: number[];
        let checkIdx: number = 0;
        switch (clearType) {
            case 1:
                clearGroup = this.cBoxConfig.clear[clearId];
                break;
            case 2:
                clearGroup = this.cBoxConfig.clearPlus[clearId];
                break;
            case 3:
                clearGroup = this.cBoxConfig.clearHandle[clearId];
                break;
        }

        clearGroup.forEach((num) => {

            const box: eui.CheckBox = this.host[`btn_rule_${num}`];

            if (box) {
                switch (clearType) {
                    case 3:
                        this.cBoxPresCheckHandle(num);
                        break;
                    default:
                        box.selected = false;
                        break;
                }
            }
        });

    }
    /**cBox联动 互斥组处理 */
    private cBoxGroupHandle(groupId: number[], idx: number, select: boolean) {

        if (groupId) {
            let slt: boolean = false;
            groupId.forEach((id) => {
                const group: number[] = this.cBoxConfig.group[id].btns;
                const def: number = this.cBoxConfig.group[id].def;
                group.forEach((num) => {
                    const box: eui.CheckBox = this.host[`btn_rule_${num}`];
                    if (box) {
                        if (num === idx) {
                            slt = select;
                        } else {
                            box.selected = false;
                            let config = this.cBoxConfig.btns[num];
                            if (config && config.clearId) {
                                this.cBoxClearhandle(config.clearId);
                            }
                        }
                    }

                });

                if (!slt && !!def && !select) {
                    let box: eui.CheckBox = this.host[`btn_rule_${group[def - 1]}`];
                    if (box) {
                        box.selected = true;
                    }
                };
            })

        }
    }
    /**cBox联动不定项的先决条件阻止选中*/
    private cBoxPresCheckHandle(idx: number): boolean {
        const checkId: number = this.cBoxConfig.btns[idx].checkId;
        const checkGroup: number[] = this.cBoxConfig.check[checkId];
        let flag: boolean = true;

        if (checkGroup) {
            const box: eui.CheckBox = this.host[`btn_rule_${idx}`];
            if (box) {

                flag = checkGroup.some((num) => {
                    let _box: eui.CheckBox = this.host[`btn_rule_${num}`];
                    if (_box) {
                        return _box.selected;
                    }
                    return false;
                });

                if (box.selected) {
                    box.selected = flag;
                }
            }
        }
        return flag;
    }


}